package com.hq.door.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hq.door.dto.Major;
import com.hq.door.dto.MajorDto;
import com.hq.door.pojo.TMajor;
import com.hq.door.service.IMajorService;

/**
 * 作者：袁健宝
 * 日期：2016年12月30日 上午5:02:58
 */
@Controller
public class MajorController {
	/*
	 http://localhost:8080/huaq/major/query
	 http://localhost:8080/huaq/major/queryAll
	 http://localhost:8080/huaq/major/add?name=%E8%A2%81%E5%81%A5%E5%AE%9D&code=123456
	 */
	
	@Autowired
	IMajorService majorService;
	
	@RequestMapping("/major/add")
	public ModelAndView addMajor(Major major) {
		System.out.println("-------MajorController----addUser---");
		System.out.println(major);
		majorService.save(MajorDto.readMajor(major));
		return new ModelAndView("/user/success");
	}
	
	@RequestMapping("/major/query")
	@ResponseBody
	public Major getMajor() {
		TMajor tmajor = majorService.get(3);
		Major major = MajorDto.writeMajor(tmajor);
		return major;
	}
	
	@RequestMapping("/major/queryAll")
	@ResponseBody
	public Major[] findPageObjects(){
		String hql = "from TMajor order by id";
		Object[] conditions = {};
		int pageNo = 1;
		int pageSize = 4;
		int pageStart = pageNo*pageSize-pageSize;
		List<TMajor> tmajors = majorService.findPageObjects(hql, pageStart, pageSize, conditions);
		//List<Major> list = MajorDto.writeMajor(tmajors);
		Major[] majors = MajorDto.writeMajor(tmajors);
		for (int i = 0; i < majors.length; i++) {
			Major major = majors[i];
			System.out.println(major);
		}
		return majors;
	}
	
}
